<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 16/6/16
 * Time: 下午4:08
 */

$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();

$test = get_maintenance_inventory_supply_detail(1,'2016-06-01 00:00:00','2016-06-16 00:00:00');

foreach ($test as $_report){
    foreach ($_report as $value){
        if(is_array($value)){
            foreach ($value as $_value){
                foreach ($_value as $v){
                    echo $v . ",";
                }
            }
            echo "\n";
        }else{
            echo $value . ",";
        }
    }
    echo "\n";
}

/*盘库明细*/
function get_maintenance_inventory_supply_detail($shop_id,$begin_date,$end_date){
    $storage_detail = array();
    $inventory_supply = MaintenanceInventorySupply::where('status',2)
        ->whereBetween('updated_at',array($begin_date,$end_date))
        ->where('shop_id',$shop_id)
        ->get();
    foreach ($inventory_supply as $_inventory_supply){
        $item_detail = Report::get_supply_detail($_inventory_supply->item_id,$_inventory_supply->item_type);
        $part_classification = Report::get_part_classification($item_detail->st_classifi_id);

        if($_inventory_supply->type == 0) $type = '盘盈';
        elseif ($_inventory_supply->type == 1) $type = '盘亏';
        elseif ($_inventory_supply->type == 2) $type = '盘平';
        else $type = $_inventory_supply->type;

        $maintenance_inventory_storage = MaintenanceInventoryStorage::where('mis_id',$_inventory_supply->id)->first();
        $maintenance_storage_item = MaintenanceStorageItem::where('id',$maintenance_inventory_storage->storage_id)->first();

        $_storage_detail = array(
            '盘点记录单ID' => $_inventory_supply->list_id,
            '门店ID' => $_inventory_supply->shop_id,
            '账面数' => $_inventory_supply->date_number,
            '实际数' => $_inventory_supply->current_number,
            '差异数' => $_inventory_supply->number,
            '盘点差异数' => $_inventory_supply->check_number,
            '成本' => $_inventory_supply->number * $maintenance_storage_item->tax_purchase_price,
            '盈亏类型' => $type,
            '创建时间' => $_inventory_supply->created_at,
            '更新时间' => $_inventory_supply->updated_at,
            '一级分类' => $part_classification['level_1_name'],
            '二级分类' => $part_classification['level_2_name'],
            '三级分类' => $part_classification['level_3_name'],
        );
        array_push($storage_detail,$_storage_detail);
    }
    echo '盘点记录单ID,门店ID,账面数,实际数,差异数,盘点差异数,成本,盈亏类型,创建时间,更新时间,一级分类,二级分类,三级分类'."\n";
    return $storage_detail;
}

/*出库明细*/
function get_maintenance_out_detail($shop_id,$begin_date,$end_date){
    $storage_detail = array();
    $list_num = 0;
    $item_num = 0;
    //获取入库单
    $storage_list = MaintenanceOutList::whereBetween('date',array($begin_date,$end_date))
        ->where('shop_id',$shop_id)
        ->where('status',1)
        ->get();
    foreach ($storage_list as $_storage_list){
        $list_num = $list_num + 1;
        $storage_item = MaintenanceOutItem::where('list_id',$_storage_list->id)
            ->where('status',1)
            ->get();

        if($_storage_list->consuming_type == 1){
            $consuming_type = '门店';
            $shop = MaintenanceShops::where('id',$_storage_list->consuming_id)->first();
            $consuming_id = $shop->name;
        }elseif($_storage_list->consuming_type == 2){
            $consuming_type = '供货商';
            $supplier = MaintenanceSupplier::where('id',$_storage_list->consuming_id)->first();
            if(is_null($supplier)) $consuming_id = '';
            else $consuming_id = $supplier->name;
        }else{
            $consuming_type = $_storage_list->consuming_type;
            $consuming_id = $_storage_list->consuming_id;
        }

        if($_storage_list->out_type == 0) $out_type = '内部领用';
        elseif ($_storage_list->out_type == 1) $out_type = '退货';
        else $out_type = $_storage_list->out_type;

        if($_storage_list->methed == 0) $methed = '领用';
        elseif ($_storage_list->methed == 1) $methed = '调出';
        else $methed = $_storage_list->methed;

        if($_storage_list->pay_type == 0) $pay_type = '现金';
        elseif ($_storage_list->pay_type == 1) $pay_type = '刷卡';
        elseif ($_storage_list->pay_type == 2) $pay_type = '挂账';
        else $pay_type = $_storage_list->pay_type;

        if($_storage_list->type == 1) $type = '内部领用';
        elseif ($_storage_list->type == 2) $type = '退货出库';
        elseif ($_storage_list->type == 3) $type = '订单出库';
        else $type = $_storage_list->type;

        foreach ($storage_item as $_storage_item){
            $item_num = $item_num + 1;
            $item_detail = Report::get_supply_detail($_storage_item->item_id,$_storage_item->item_type);
            $part_classification = Report::get_part_classification($item_detail->st_classifi_id);

            $_storage_detail = array(
                '出库单ID' => $_storage_list->id,
                '门店' => $_storage_list->shop_id,
                '出库时间' => $_storage_list->date,
                '出库对象' => $consuming_type,
                '出库对象名称' => $consuming_id,
                '出库方式' => $out_type,
                '领用方式' => $methed,
                '结算方式' => $pay_type,
                '类型' => $type,
                '备注' => $_storage_list->desc,
                '商品编码' => $_storage_item->code,
                '商品名称' => $_storage_item->name,
                '含税进价' => $_storage_item->tax_purchase_price,
                '无税进价' => $_storage_item->no_tax_purchase_price,
                '含税小计' => $_storage_item->tax_purchase_subtotal,
                '无税计划成本' => $_storage_item->no_tax_plan_costs,
                '含税计划成本' => $_storage_item->tax_plan_costs,
                '出库数量' => $_storage_item->number,
                '退库数量' => $_storage_item->return_numer,
                '品类' => $_storage_item->subject,
                '货位' => $_storage_item->location,
                '包装' => $_storage_item->pack_unit,
                '型号' => $_storage_item->model,
                '创建时间' => $_storage_item->created_at,
                '更新时间' => $_storage_item->updated_at,
                '一级分类' => $part_classification['level_1_name'],
                '二级分类' => $part_classification['level_2_name'],
                '三级分类' => $part_classification['level_3_name'],
            );
            array_push($storage_detail,$_storage_detail);
        }
    }
    echo 'list num :' . $list_num . "\n";
    echo 'item num :' . $item_num . "\n";
    echo '出库单ID,门店,出库时间,出库对象,出库对象名称,出库方式,领用方式,结算方式,类型,备注,商品编码,商品名称,含税进价,无税进价,含税小计,无税计划成本,含税计划成本,出库数量,退库数量,品类,货位,包装,型号,创建时间,更新时间,一级分类,二级分类,三级分类'."\n";
    return $storage_detail;
}

/*入库商品明细*/
function get_maintenance_storage_detail($shop_id,$begin_date,$end_date){
    $storage_detail = array();
    $list_num = 0;
    $item_num = 0;
    //获取入库单
    $storage_list = MaintenanceStorageList::whereBetween('date',array($begin_date,$end_date))
        ->where('shop_id',$shop_id)
        ->where('status',1)
        ->get();
    foreach ($storage_list as $_storage_list){
        $list_num = $list_num + 1;
        $storage_item = MaintenanceStorageItem::where('list_id',$_storage_list->id)
            ->where('status',1)
            ->get();

        $supplier = MaintenanceSupplier::where('id',$_storage_list->supplier)->first();
        if(is_null($supplier)) $supplier_name = '';
        else $supplier_name = $supplier->name;


        if($_storage_list->storage_type == 0) $storage_type = '购入';
        elseif ($_storage_list->storage_type == 1) $storage_type = '调入';
        elseif ($_storage_list->storage_type == 2) $storage_type = '订单退货';
        elseif ($_storage_list->storage_type == 3) $storage_type = '内部领用';
        else $storage_type = $_storage_list->storage_type;

        if($_storage_list->methed == 0) $methed = '领用';
        elseif ($_storage_list->methed == 1) $methed = '调出';
        else $methed = $_storage_list->methed;

        if($_storage_list->pay_type == 0) $pay_type = '现金';
        elseif ($_storage_list->pay_type == 1) $pay_type = '刷卡';
        elseif ($_storage_list->pay_type == 2) $pay_type = '挂账';
        else $pay_type = $_storage_list->pay_type;

        if($_storage_list->type == 1) $type = '批量临时';
        elseif ($_storage_list->type == 2) $type = '领用归还';
        elseif ($_storage_list->type == 3) $type = '订单退货';
        elseif ($_storage_list->type == 4) $type = '零售退货';
        else $type = $_storage_list->type;

        foreach ($storage_item as $_storage_item){
            $item_num = $item_num + 1;
            $item_detail = Report::get_supply_detail($_storage_item->item_id,$_storage_item->item_type);
            $part_classification = Report::get_part_classification($item_detail->st_classifi_id);

            if($_storage_item->type == 1) $item_type = '入库';
            elseif ($_storage_item->type == 2) $item_type = '退库';
            else $item_type = $_storage_item->type;

            $_storage_detail = array(
                '入库单ID' => $_storage_list->id,
                '门店' => $_storage_list->shop_id,
                '入库时间' => $_storage_list->date,
                '供货商' => $supplier_name,
                '入库方式' => $storage_type,
                '领用门店ID' => $_storage_list->consuming_id,
                '领用方式' => $methed,
                '结算方式' => $pay_type,
                '类型' => $type,
                '备注' => $_storage_list->desc,
                '商品编码' => $_storage_item->code,
                '商品名称' => $_storage_item->name,
                '含税进价' => $_storage_item->tax_purchase_price,
                '无税进价' => $_storage_item->no_tax_purchase_price,
                '含税小计' => $_storage_item->tax_purchase_subtotal,
                '无税计划成本' => $_storage_item->no_tax_plan_costs,
                '含税计划成本' => $_storage_item->tax_plan_costs,
                '入库数量' => $_storage_item->number,
                '当前数量' => $_storage_item->current_number,
                '品类' => $_storage_item->subject,
                '货位' => $_storage_item->location,
                '单项类型' => $item_type,
                '包装' => $_storage_item->pack_unit,
                '型号' => $_storage_item->model,
                '创建时间' => $_storage_item->created_at,
                '更新时间' => $_storage_item->updated_at,
                '一级分类' => $part_classification['level_1_name'],
                '二级分类' => $part_classification['level_2_name'],
                '三级分类' => $part_classification['level_3_name'],
                '商品类型' => $_storage_item->item_type,
                '商品id' => $_storage_item->item_id,
            );
            array_push($storage_detail,$_storage_detail);
        }
    }
    echo 'list num :' . $list_num . "\n";
    echo 'item num :' . $item_num . "\n";
    echo '入库单ID,门店,入库时间,供货商,入库方式,领用门店ID,领用方式,结算方式,类型,备注,商品编码,商品名称,含税进价,无税进价,含税小计,无税计划成本,含税计划成本,入库数量,当前数量,品类,货位,单项类型,包装,型号,创建时间,更新时间,一级分类,二级分类,三级分类'."\n";
    return $storage_detail;
}